var SJTabDefault = new Class({
	options:{		
	},
	initialize:function(options){
		this.setOptions(options); 
		this.container 	= $(this.options.container); 
		this.tabTitles 	= $$('div#'+this.options.container+' ul.tabs_title li');
		this.panel 		= $$('div#'+this.options.container+' div.st_default_wrap2')[0]; 
		this.tabPanels 	= $$('div#'+this.options.container+' .st_default_content'); 
		this.currentTab = 0; 
		this.fx = new Fx.Elements(this.tabPanels, {duration:this.options.duration});
		this.effectType = this.options.effectType;
		
		this.firstLoad();
		if(this.options.isModule == 0) this.hoverItem();
	},
	firstLoad:function(){
		switch(this.effectType){
			case "scroll_tb":			
				this.initScrollTopBottom();
			break;
			case "scroll_lr":
				this.initScrollLeftRight();
			break;	
			case "st_news2":
				this.initSJNews2();
			break;
			case "fade":
				this.initSJFade();
			break;	
		}
	},
	initSJFade:function(){
		this.tabPanels.each(function(item,i){
			if(i!=0){
				item.setStyle('opacity',0);
				item.setStyle('visibility','hidden');
			} else {
				item.setStyle('opacity',1);
				item.setStyle('visibility','visible');
			}
			this.tabTitles[i].addEvent(this.options.eventType,function(){
				this.changeTabFade(i);
			}.bind(this));
		}.bind(this));
		if(this.options.height>0){
			this.panel.setStyle('height',this.options.height);
		}else{
			this.panel.setStyle('height',this.tabPanels[0].offsetHeight);
		} 		
	},
	changeTabFade:function(i){
		if(this.currentTab != i ){ 
			var obj = {}; 
			this.tabPanels.each(function(item,j){
				if(i!=j){
					this.tabTitles[j].removeClass('active');
					obj[j] = {'opacity':0 };  
					item.setStyle('visibility','hidden');
				} else {
					this.tabTitles[i].addClass('active');
					obj[j] = {'opacity':1 };  
					item.setStyle('visibility','visible');
					if(this.options.height>0){
						this.panel.setStyle('height',this.options.height);
					}else{
						this.panel.setStyle('height',this.tabPanels[j].offsetHeight);
					}
				}
			}.bind(this)); 
			this.fx.start(obj); 
			this.currentTab = i;
		} 
	},
	initSJNews2:function(){
		this.tabPanels[0].setStyle('visibility','visible');
		this.tabPanels[0].setStyle('opacity','1');
		if(this.options.height>0){
			this.panel.setStyle('height',this.options.height);
		}else{
			this.panel.setStyle('height',this.tabPanels[0].offsetHeight);
		} 
		this.tabTitles.each(function(item,i){
			item.addEvent(this.options.eventType,function(){this.changeTabSJNews2(i);}.bind(this));
		}.bind(this));
	},
	initScrollTopBottom:function(){
		var top_height = 0;
		this.tabPanels.each(function(item, i){
			item.setStyle('visibility', 'visible');
			item.setStyle('left', '0px');
			item.setStyle('top', top_height+'px');
			top_height += item.offsetHeight; 
			this.tabTitles[i].addEvent(this.options.eventType, function(){this.changeTabScrollTb(i);}.bind(this));
		}.bind(this));
		if(this.options.height>0){
			this.panel.setStyle('height',this.options.height);
		}else{
			this.panel.setStyle('height',this.tabPanels[0].offsetHeight);
		} 
	},
	initScrollLeftRight:function(){	
		var left_width = 0;
		this.tabPanels.each(function(item,i){			
			item.setStyle('visibility', 'visible');
			item.setStyle('left', left_width + 'px');
			item.setStyle('top', '0px');
			left_width += item.offsetWidth; 			
			this.tabTitles[i].addEvent(this.options.eventType,function(){this.changeTabScrollLr(i);}.bind(this));
		}.bind(this));
		if(this.options.height>0){
			this.panel.setStyle('height',this.options.height);
		}else{
			this.panel.setStyle('height',this.tabPanels[0].offsetHeight);
		} 
	},
	changeTabSJNews2:function(i){
		if(this.currentTab != i){					
			this.tabPanels.each(function(item,j){
				if(j!=i){ 
					this.tabPanels[j].setStyle('visibility','hidden');
					this.tabPanels[j].setStyle('opacity','0');
					this.tabTitles[j].removeClass('active');					
				} else { 
					this.tabTitles[j].addClass('active');
					this.tabPanels[j].setStyle('visibility','visible');
					this.tabPanels[j].setStyle('opacity','1');
					if(this.options.height>0){
						this.panel.setStyle('height',this.options.height);
					}else{
						this.panel.setStyle('height',this.tabPanels[j].offsetHeight+5);
					}  			
				} 
			}.bind(this));		
			this.currentTab = i;
		}
	},
	changeTabScrollLr:function(i){
		if(this.currentTab != i){
			var obj = {}; 
			var offset = this.tabPanels[i].offsetLeft.toInt();			
			this.tabPanels.each(function(item,j){
				if(j!=i){
					this.tabTitles[j].removeClass('active');					
				} else {										
					this.tabTitles[j].addClass('active');
					if(this.options.height>0){
						this.panel.setStyle('height',this.options.height);
					}else{
						this.panel.setStyle('height',this.tabPanels[j].offsetHeight+5);
					}  			
				}
				obj[j] = {'left':[item.offsetLeft.toInt(), item.offsetLeft.toInt()- offset]};				
			}.bind(this));		
			this.fx.start(obj);
			this.currentTab = i;
		}
	},
	changeTabScrollTb:function(i){	
		if(this.currentTab != i){
			var obj = {}; 
			var offset = this.tabPanels[i].offsetTop.toInt();			
			this.tabPanels.each(function(item,j){
				if(j!=i){
					this.tabTitles[j].removeClass('active');					
				} else {										
					this.tabTitles[j].addClass('active');
					if(this.options.height>0){
						this.panel.setStyle('height',this.options.height);
					}else{
						this.panel.setStyle('height',this.tabPanels[j].offsetHeight+5);
					}  		
				}
				obj[j] = {'top':[item.offsetTop.toInt(), item.offsetTop.toInt()- offset]};				
			}.bind(this));		
			this.fx.start(obj);
			this.currentTab = i;
		}
	},
	hoverItem:function(){
		this.items = $$('div#'+this.options.container+' div.panel_tr li.st_default_item');
		this.items.each(function(item,i){			
			item.addEvent('mouseenter',function(){item.addClass('item_active');});
			item.addEvent('mouseleave',function(){item.removeClass('item_active');});				
		}.bind(this));
	}
});
SJTabDefault.implement(new Events);
SJTabDefault.implement(new Options);